package cs4game;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.apache.xerces.jaxp.DocumentBuilderFactoryImpl;
import org.apache.xerces.parsers.DOMParser;
import org.lwjgl.opengl.GL11;
import org.newdawn.slick.opengl.Texture;
import org.newdawn.slick.opengl.TextureLoader;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class Style { //Gangnam

	public static HashMap<String, Style> styleMap = new HashMap<String, Style>();

	public static void buildStyleMap(String fileName){
		DOMParser parser = new DOMParser();
		try{
			parser.parse(fileName);
		} catch(SAXException e){
			System.err.println(fileName + " is not well-formed.");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			System.err.println("Error reading style xml file");
		}
		
		Node document = parser.getDocument();
		for(int i = 0; i<document.getChildNodes().getLength(); i++){
			Node docCh = document.getChildNodes().item(i);
			if(docCh.getNodeName().equals("style")){
				Style newStyle = new Style();
				for(int j = 0; j<docCh.getChildNodes().getLength(); j++){
					Node styleChild = docCh.getChildNodes().item(j);
					if(styleChild.getNodeName().equals("file"))
						newStyle.setFileName(styleChild.getAttributes().getNamedItem("filename").getNodeValue());
					if(styleChild.getNodeName().equals("floor")){
						
						for(int k = 0; k<styleChild.getChildNodes().getLength(); k++){
							Node floorChild = styleChild.getChildNodes().item(k);
							if(floorChild.getNodeName().equals("tile")){
								String c = floorChild.getAttributes().getNamedItem("coordinate").getNodeValue();
								newStyle.getFloor().add(new TexPoint(c));
							}
						}
					}
					if(styleChild.getNodeName().equals("wall")){
						for(int k = 0; k<styleChild.getChildNodes().getLength(); k++){
							Node wallChild = styleChild.getChildNodes().item(k);
							if(wallChild.getNodeName().equals("tile")){
								String c = wallChild.getAttributes().getNamedItem("coordinate").getNodeValue();
								newStyle.getWall().add(new TexPoint(c));
							}
						}
					}
				}
				styleMap.put(docCh.getAttributes().getNamedItem("name").getNodeValue(), newStyle);
				System.out.println(styleMap.get("ship1"));
			}
		}
	}


	private String fileName;
	private Texture tex;
	private ArrayList<TexPoint> floor;
	private ArrayList<TexPoint> wall;
	private double tileSize = 0.25;

	public Style(){
		floor = new ArrayList<TexPoint>();
		wall = new ArrayList<TexPoint>();
	}
	
	public Style(String fN, ArrayList<TexPoint> f, ArrayList<TexPoint> w){
		setFileName(fN);
		setFloor(f);
		setWall(w);
	}
	
	public void loadTexture(){
		try {
			tex = TextureLoader.getTexture("PNG", new FileInputStream(fileName));
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		tex.bind();
	}
	
	public void unloadTexture(){
		tex.release();
		tex = null;
	}
	
	public String toString(){
		String s = "";
		s+=("File: "+fileName+"\n");
		s+=("Floors: \n");
		for(TexPoint t: floor){
			s+=("     "+t+"\n");
		}
		s+=("Walls: \n");
		for(TexPoint t: wall){
			s+=("     "+t+"\n");
		}
		return s;
	}


	

	public static void main(String[] arrrrrrrrrgs){
		Style.buildStyleMap("xml/styles.xml");
	}

	public String getFileName() {
		return fileName;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
	}

	private ArrayList<TexPoint> getFloor() {
		return floor;
	}

	private void setFloor(ArrayList<TexPoint> floor) {
		this.floor = floor;
	}

	private ArrayList<TexPoint> getWall() {
		return wall;
	}

	private void setWall(ArrayList<TexPoint> wall) {
		this.wall = wall;
	}

	public int getNumFloors() {
		return floor.size();
	}

	public TexPoint[] getFloorTexCoords(int index) { //toReturn[0] is topleft, toReturn[1] is bottomright
		return new TexPoint[]{floor.get(index), new TexPoint(floor.get(index).x+tileSize, floor.get(index).y+tileSize)};		
	}

	public double getTileSize() {
		return tileSize;
	}
	
}
